import sqlite3
import os
import sys
import time

# 检查db/sync.db文件
db_path = os.path.join('db', 'sync.db')
print(f"检查数据库: {db_path}")
print(f"数据库文件是否存在: {os.path.exists(db_path)}")

if os.path.exists(db_path):
    try:
        conn = sqlite3.connect(db_path)
        conn.row_factory = sqlite3.Row
        cursor = conn.cursor()
        
        # 获取所有表
        print("\n数据库中的表:")
        cursor.execute("SELECT name FROM sqlite_master WHERE type='table'")
        tables = cursor.fetchall()
        for table in tables:
            print(f"- {table['name']}")
        
        # 获取所有同步目录
        print("\n所有同步目录:")
        try:
            cursor.execute("SELECT * FROM sync_dirs")
            sync_dirs = cursor.fetchall()
            if sync_dirs:
                for dir_info in sync_dirs:
                    print(f"ID: {dir_info['id']}")
                    print(f"用户ID: {dir_info['user_id']}")
                    print(f"本地路径: {dir_info['local_path']}")
                    print(f"FTP主机: {dir_info['ftp_host']}")
                    print(f"FTP用户: {dir_info['ftp_user']}")
                    print(f"FTP密码: {dir_info['ftp_pass']}")
                    print(f"FTP路径: {dir_info['ftp_path']}")
                    print(f"同步策略: {dir_info['sync_strategy']}")
                    print("-" * 30)
                    sys.stdout.flush()  # 确保输出被刷新
            else:
                print("没有同步目录记录")
        except Exception as e:
            print(f"查询sync_dirs表出错: {e}")
        
        # 特别查询ID为11和16的记录
        print("\n特别查询ID为11和16的记录:")
        try:
            cursor.execute("SELECT * FROM sync_dirs WHERE id IN (11, 16)")
            specific_dirs = cursor.fetchall()
            if specific_dirs:
                for dir_info in specific_dirs:
                    print(f"ID: {dir_info['id']}")
                    print(f"FTP主机: {dir_info['ftp_host']}")
                    print(f"本地路径: {dir_info['local_path']}")
                    print(f"FTP路径: {dir_info['ftp_path']}")
                    print("-" * 30)
                    sys.stdout.flush()  # 确保输出被刷新
            else:
                print("没有找到ID为11或16的记录")
        except Exception as e:
            print(f"查询特定ID记录出错: {e}")
        
        conn.close()
    except Exception as e:
        print(f"处理数据库时出错: {e}")
else:
    print("数据库文件不存在")

# 检查是否有硬编码的FTP服务器地址
print("\n检查是否有硬编码的FTP服务器地址:")
try:
    with open('sync_manager.py', 'r', encoding='utf-8') as f:
        content = f.read()
        if '192.168.201.51' in content:
            print("在sync_manager.py中找到硬编码的192.168.201.51")
        if '192.168.201.54' in content:
            print("在sync_manager.py中找到硬编码的192.168.201.54")
except Exception as e:
    print(f"读取sync_manager.py失败: {e}")

try:
    with open('file_syncer.py', 'r', encoding='utf-8') as f:
        content = f.read()
        if '192.168.201.51' in content:
            print("在file_syncer.py中找到硬编码的192.168.201.51")
        if '192.168.201.54' in content:
            print("在file_syncer.py中找到硬编码的192.168.201.54")
except Exception as e:
    print(f"读取file_syncer.py失败: {e}")

try:
    with open('app.py', 'r', encoding='utf-8') as f:
        content = f.read()
        if '192.168.201.51' in content:
            print("在app.py中找到硬编码的192.168.201.51")
        if '192.168.201.54' in content:
            print("在app.py中找到硬编码的192.168.201.54")
except Exception as e:
    print(f"读取app.py失败: {e}")

print("\n脚本执行完毕")
sys.stdout.flush()  # 确保最终输出被刷新
time.sleep(1)  # 等待一秒，确保所有输出都显示 